package com.tyd.cams.zixunfragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.tyd.cams.Adapter.TzggAdapter;
import com.tyd.cams.R;
import com.tyd.cams.SchoolZiXunActivity;
import com.tyd.cams.entity.SkTzgg;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;

import okhttp3.Call;

public class SchoolZiXunFragment extends Fragment {
    ArrayList<SkTzgg> list = new ArrayList<>();
    private ListView listView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getXuexiaoZX();
        return inflater.inflate(R.layout.fragment_school_zi_xun, container, false);
    }

    /*获取学校资讯*/
    private void getXuexiaoZX() {
        new Thread() {
            public void run() {
                OkHttpUtils.post()
                        .url("http://syist.cn/News/index/cid/")
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                Toast.makeText(getContext(), "沈科新闻链接服务器超时", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onResponse(String response, int id) {
//                                Log.i("获取的返回信息", response);
                                Document document = Jsoup.parse(response);
                                Elements context = document.select("body > div.main > div.p-r > div.mainbox>  ul > li");
//                                String imgurl = document.select("#picbox > li").select("img").attr("src");
                                for (Element element : context) {
                                    String time = element.select("b > span").text();
                                    String title = element.select("b > a").text();
                                    String con = element.select("p").text();
                                    String href = element.select("b > a").attr("href");
                                    SkTzgg tzgg = new SkTzgg(time, title, con, "http://www.syist.cn/" + href);
                                    list.add(tzgg);
                                }
                                listView = getActivity().findViewById(R.id.ZXListView);
                                TzggAdapter tzggAdapter = new TzggAdapter(getContext(), list);
                                listView.setAdapter(tzggAdapter);
                                tzggAdapter.notifyDataSetChanged();
                                Log.i("学校资讯AND*沈科新闻*信息", list.toString());
                                for (SkTzgg skTzgg : list) {
                                    System.out.println(skTzgg);
                                }

                            }
                        });
            }
        }.start();
    }

}